package org.eiahe.hr.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.Shift;
import org.eiahe.hr.attendance.domain.bo.ShiftBo;
import org.eiahe.hr.attendance.domain.vo.ShiftVo;
import org.eiahe.hr.attendance.mapper.ShiftMapper;
import org.eiahe.hr.attendance.service.IShiftService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 班次服务实现
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class ShiftServiceImpl implements IShiftService {

    private final ShiftMapper baseMapper;

    /**
     * 查询班次
     */
    @Override
    public ShiftVo queryById(Long shiftId) {
        return baseMapper.selectVoById(shiftId);
    }

    /**
     * 查询班次列表
     */
    @Override
    public List<ShiftVo> queryList(ShiftBo bo) {
        LambdaQueryWrapper<Shift> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 分页查询班次列表
     */
    @Override
    public TableDataInfo<ShiftVo> queryPageList(ShiftBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Shift> lqw = buildQueryWrapper(bo);
        Page<ShiftVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<Shift> buildQueryWrapper(ShiftBo bo) {
        LambdaQueryWrapper<Shift> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getShiftName()), Shift::getShiftName, bo.getShiftName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Shift::getStatus, bo.getStatus());
        lqw.orderByAsc(Shift::getCreateTime);
        return lqw;
    }

    /**
     * 新增班次
     */
    @Override
    public Boolean insertByBo(ShiftBo bo) {
        Shift add = MapstructUtils.convert(bo, Shift.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setShiftId(add.getShiftId());
        }
        return flag;
    }

    /**
     * 修改班次
     */
    @Override
    public Boolean updateByBo(ShiftBo bo) {
        Shift update = MapstructUtils.convert(bo, Shift.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Shift entity) {
        // 可以添加业务校验,比如上班时间必须早于下班时间等
        if (entity.getWorkStartTime() != null && entity.getWorkEndTime() != null) {
            if (entity.getWorkStartTime().isAfter(entity.getWorkEndTime())) {
                throw new RuntimeException("上班时间不能晚于下班时间");
            }
        }
        if (entity.getLunchStartTime() != null && entity.getLunchEndTime() != null) {
            if (entity.getLunchStartTime().isAfter(entity.getLunchEndTime())) {
                throw new RuntimeException("午休开始时间不能晚于午休结束时间");
            }
        }
    }

    /**
     * 批量删除班次
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        // 可以添加删除前的校验,比如检查是否有员工在使用该班次
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
